import DataBus from "../../databus";
import Utils from "../../common/utils";
import {PageStatus} from "../../page_status";
import XmlManager from "../../manager/xml_manager";
import Scene from "../../runtime/scene";
import Text from "../../base/text";

const databus = new DataBus()
import { winW, winH, px } from '../../libs/utils.js';
import {Circle, CircleText, Ellipse, Rect, RectText} from "../../base/shape";
import ImgText from "../../base/img_text";
import AlbumView from "../../runtime/album";
import JsLogic from "../../runtime/jslogic";
import Img from "../../base/img";
import Block from "../../base/block";
import Cans from "../../base/cans";
import Group from "../../base/group";
var jsLogic = new JsLogic();
/**
 * 旅行主页面
 */
export default class MainMenu extends Scene{
  constructor() {
    super(0, 0, winW, winH);
    this.albumView = null;
  }

  initView(ctx){
    setTimeout(() => {

      var imgBg = new Img("js/sub/images/main/img_zhujiemian_board.png", 0, 0, winW, winH);
      this.addChild(imgBg, "imgBg")

      var btnAlbum = new Img("js/sub/images/main/btn_xiangce.png", px(100), px(300), px(353), px(273));
      this.addChild(btnAlbum, "btnAlbum")

      var btnTrip = new Img("js/sub/images/main/btn_lvxing.png",  px(250),  px(600), px(303), px(328));
      this.addChild(btnTrip, "btnTrip")




      btnTrip.bindClickEvent(()=>{
        databus.gameStatus = PageStatus.TripShow;
        wx.setStorage({
          key:"lastPage",
          data:PageStatus.TripShow
        })
      });

      btnAlbum.bindClickEvent(()=>{
        if(this.albumView !== null && this.albumView.visible === true){
          return;
        }
        var imgList = ["js/sub/images/postcard/r_1_1_0.png",
          "js/sub/images/postcard/r_1_2_0.png",
          "js/sub/images/postcard/r_1_3_0.png",
          "js/sub/images/postcard/r_1_4_0.png",
          "js/sub/images/postcard/r_1_5_0.png",
          "js/sub/images/postcard/r_1_6_0.png",
          "js/sub/images/postcard/r_1_7_0.png",
          "js/sub/images/postcard/r_1_8_0.png",
          "js/sub/images/postcard/r_1_9_0.png",
          "js/sub/images/postcard/r_1_10_0.png",
          "js/sub/images/postcard/r_1_11_0.png",
          "js/sub/images/postcard/r_1_12_0.png"]
        this.showAlbumView(imgList);
      });

      this.initMenu(ctx);
      this.addDebugView()

      if(jsLogic.getCansStatus() === true){
        var cans = new Cans(30, winH-170,winW-60, 120,()=>{
          console.log("互动了")
          var reapFish = jsLogic.reapFish();
          if(reapFish === true){
            cans.hideCans()
            this.userWalletText.text = `玩家持有: ${jsLogic.getUserMoney()}`

          }

        });
        this.addChild(cans, "cans")
      }


    }, 0);

  }


  initMenu(ctx) {
    var imgUserWalletBg = new Img("js/sub/images/img_wjcy_db.png", winW - px(266) - 20, px(155), px(266), px(60), px(266), (60));
    this.addChild(imgUserWalletBg, "imgUserWalletBg")

    var imgUserWalletFish = new Img("js/sub/images/icon_yugan.png", winW - px(266) - 30, px(160), px(62), px(48), px(62), (48));
    this.addChild(imgUserWalletFish, "imgUserWalletFish")

    //// 猫咪状态
    var imgCatWalletBg = new Img("js/sub/images/img_fy_db.png", 0, px(150), px(312), px(76), px(312), (76));
    this.addChild(imgCatWalletBg, "imgCatWalletBg")
    //// 猫咪没钱的状态
    var imgCat = new Img("js/sub/images/img_fuyu_gao.png", 20, px(155), px(92), px(64), px(92), (64));
    this.addChild(imgCat, "imgCat")

    this.catWalletText = new Text(jsLogic.info.show.cur_el_name, px(92) + 30, px(170), px(30))
    this.addChild(this.catWalletText, "catWalletText")

    this.userWalletText = new Text(`玩家持有: ${jsLogic.info.base.user_money}`, winW - px(266 - 62) - 30, px(170), px(28))
    this.addChild(this.userWalletText, "userWalletText")
  }

  update() {
    super.update();
    if (databus.gameStatus !== PageStatus.GameMainMenu) {
      return;
    }

  }

  render(ctx) {
    if (databus.gameStatus !== PageStatus.GameMainMenu) {
      return;
    }

    super.render(ctx);
  }
  onTouchEvent(type, x, y, callback) {
      
    switch (type) {
      case 'touchstart':
        break
    }
  }
  showAlbumView(imgList) {

    if (this.albumView == null) {

      this.albumView = new AlbumView(0, 0, winW, winH, imgList)
      this.albumView.setCallback(() => {
        this.hideAlbumView();
      });
      this.albumView.bgColor ='rgba(0,0,0,1)';
      this.addChild(this.albumView, "albumView")
    }else{
      this.albumView.visible = true;
      this.albumView.setImgList(imgList)
    }

  }

  hideAlbumView() {

    this.albumView.visible = false;
  }

  emptyChild() {
    super.emptyChild();
    this.albumView = null;
  }

}
